package tech.lq0.dreamaticvoyage.init;

import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.network.IContainerFactory;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuFermenterBlockEntity;
import tech.lq0.dreamaticvoyage.gui.menu.*;
import tech.lq0.dreamaticvoyage.gui.slot.SimpleEnergyData;

public class MenuTypeRegistry {

    public static final DeferredRegister<MenuType<?>> MENU_TYPES = DeferredRegister.create(ForgeRegistries.MENU_TYPES, Utils.MOD_ID);

    public static final RegistryObject<MenuType<FukamizuCompressorMenu>> FUKAMIZU_COMPRESSOR_MENU =
            MENU_TYPES.register("fukamizu_compressor_menu",
                    () -> IForgeMenuType.create((windowId, inv, data) -> new FukamizuCompressorMenu(windowId, inv)));
    public static final RegistryObject<MenuType<PhantasmalVoyagerMenu>> PHANTASMAL_VOYAGER_MENU =
            MENU_TYPES.register("phantasmal_voyager_menu",
                    () -> IForgeMenuType.create((windowId, inv, data) -> new PhantasmalVoyagerMenu(windowId, inv)));
    public static final RegistryObject<MenuType<CrystalPopperMenu>> CRYSTAL_POPPER_MENU =
            MENU_TYPES.register("crystal_popper_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new CrystalPopperMenu(windowId, inv))));
    public static final RegistryObject<MenuType<CrystalPurifierMenu>> CRYSTAL_PURIFIER_MENU =
            MENU_TYPES.register("crystal_purifier_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new CrystalPurifierMenu(windowId, inv))));
    public static final RegistryObject<MenuType<CrystalCutterMenu>> CRYSTAL_CUTTER_MENU =
            MENU_TYPES.register("crystal_cutter_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new CrystalCutterMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuCrusherMenu>> FUKAMIZU_CRUSHER_MENU =
            MENU_TYPES.register("fukamizu_crusher_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuCrusherMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuBreadFurnaceMenu>> FUKAMIZU_BREAD_FURNACE_MENU =
            MENU_TYPES.register("fukamizu_bread_furnace_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuBreadFurnaceMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuPylonMenu>> FUKAMIZU_PYLON_MENU =
            MENU_TYPES.register("fukamizu_pylon_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuPylonMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuReactingChamberMenu>> FUKAMIZU_REACTING_CHAMBER_MENU =
            MENU_TYPES.register("fukamizu_reacting_chamber_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuReactingChamberMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuTurbineEngineMenu>> FUKAMIZU_TURBINE_ENGINE_MENU =
            MENU_TYPES.register("fukamizu_turbine_engine_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuTurbineEngineMenu(windowId, inv))));
    public static final RegistryObject<MenuType<UmisuPylonMenu>> UMISU_PYLON_MENU =
            MENU_TYPES.register("umisu_pylon_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new UmisuPylonMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuPunchMenu>> FUKAMIZU_PUNCH_MENU =
            MENU_TYPES.register("fukamizu_punch_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuPunchMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuFermenterMenu>> FUKAMIZU_FERMENTER_MENU =
            MENU_TYPES.register("fukamizu_fermenter_menu",
                    () -> new MenuType<>((IContainerFactory<FukamizuFermenterMenu>) (id, inventory, buf) -> {
                        var pos = buf.readBlockPos();
                        var blockEntity = inventory.player.getCommandSenderWorld().getBlockEntity(pos);
                        if (!(blockEntity instanceof FukamizuFermenterBlockEntity fukamizuFermenterBlockEntity)) {
                            return null;
                        }
                        return new FukamizuFermenterMenu(id, inventory.player.getInventory(), fukamizuFermenterBlockEntity, new SimpleEnergyData(FukamizuFermenterBlockEntity.MAX_DATA_COUNT));
                    }, FeatureFlags.DEFAULT_FLAGS)
            );
    public static final RegistryObject<MenuType<FukamizuExtractionMachineMenu>> FUKAMIZU_EXTRACTION_MACHINE_MENU =
            MENU_TYPES.register("fukamizu_extraction_machine_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuExtractionMachineMenu(windowId, inv))));
    public static final RegistryObject<MenuType<FukamizuBoosterMenu>> FUKAMIZU_BOOSTER_MENU =
            MENU_TYPES.register("fukamizu_booster_menu",
                    () -> IForgeMenuType.create(((windowId, inv, data) -> new FukamizuBoosterMenu(windowId, inv))));
}
